#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/10/14  13:33
# @Author: 余浪人
# @email: yulangren520@gmail.com
import requests, json
from fake_useragent import UserAgent
from flask import request, jsonify, current_app
from flask_login import login_required
from apps.control import sys_bp
from apps.lib.public import redis_
from apps.lib.token import Verify_authorization
from apps.lib.verifyCode import createMain
@sys_bp.route('/createUrl', endpoint='createUrl', methods=['POST'])
@login_required
@Verify_authorization
def createUrl():
    try:
        OOOO0OO00OO00OOO0 = request.form.get('url', '')
        OO0OOOO00000O00O0 = int(request.form.get('cls', 0))
        O00OOO000OO0O0000 = UserAgent()
        if not isinstance(OO0OOOO00000O00O0, int): return jsonify({'status': 1, 'msg': '生成格式错误!'})
        if OO0OOOO00000O00O0 == 0:
            OO0OOO0OO0OO000O0 = 'dwz.lc'
        elif OO0OOOO00000O00O0 == 1:
            OO0OOO0OO0OO000O0 = 'ons.red'
        elif OO0OOOO00000O00O0 == 2:
            OO0OOO0OO0OO000O0 = 'soso.bz'
        elif OO0OOOO00000O00O0 == 3:
            OO0OOO0OO0OO000O0 = 'maa.la'
        elif OO0OOOO00000O00O0 == 4:
            OO0OOO0OO0OO000O0 = 'ecx.cx'
        else:
            OO0OOO0OO0OO000O0 = 'did.red'
        O0O00OOO00O0OOOOO = 'https://www.xyixy.com/shorten'
        OOO000O00O00000O0 = {'url': '', 'urls': OOOO0OO00OO00OOO0, 'domain': f'https://{OO0OOO0OO0OO000O0}', 'multiple': '1', 'ocation[]': '海南'}
        O0000O000O000O0O0 = {'User-Agent': O00OOO000OO0O0000.random}
        O00O00O0OOO00OOO0 = requests.post(O0O00OOO00O0OOOOO, data=OOO000O00O00000O0, headers=O0000O000O000O0O0)
        if O00O00O0OOO00OOO0.status_code == 200 and O00O00O0OOO00OOO0.json().get('error') == 0:
            O00000O00O0OOO000 = O00O00O0OOO00OOO0.json()['short']
            if O00000O00O0OOO000:
                return jsonify({'status': 0, 'msg': '生成成功!', 'url': O00000O00O0OOO000})
        return jsonify({'status': 1, 'msg': '生成失败!'})
    except:
        return jsonify({'status': 1, 'msg': '生成异常!'})
@sys_bp.route('/verifyCode_<int:width>_<int:height>', endpoint='createVerify', methods=['GET'])
def createVerify(width: int, height: int):
    OO000OO0OOO0000OO, O0OO0000O0O0O00OO = createMain(width, height)
    OOOOOO0OOO0OO0O0O = redis_(db=0)
    OOOOOO0OOO0OO0O0O.set(f'tl_{request.remote_addr}', json.dumps(OO000OO0OOO0000OO), current_app.config['VERIFY_CODE_TIME'] * 60)
    return jsonify({'status': 0, 'imgCode': O0OO0000O0O0O00OO})
